package com.fjwt.gz.mch.ctrl.address;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.FansAddressDTO;
import com.fjwt.gz.db.vo.FansAddressVO;
import com.fjwt.gz.mch.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.FansAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api/fans")
public class FansAddressController extends CommonCtrl {

    @Autowired
    private FansAddressService fansAddressService;

    /**
     * 获取用户地址列表
     **/
    @PostMapping("/getAddressList")
    public ApiRes getAddressList() {
        Long fansId = getCurrentUser().getFansId();
        String appId = request.getHeader("appId");
        IPage<FansAddressVO> page = fansAddressService.getAddressList(getIPage(), fansId ,appId);
        log.info("C端获取用户地址列表接口：/api/fans/getAddressList,返回结果：{}", page);
        return ApiRes.page(page);
    }

    /**
     * 新增用户地址
     **/
    @PostMapping("/addAddress")
    public ApiRes addAddress() {
        String appId = getCurrentUser().getAppId();
        Long fansId = getCurrentUser().getFansId();
        FansAddressDTO dto = getObject(FansAddressDTO.class);
        dto.setAppId(appId);
        dto.setFansId(fansId);
        log.info("C端新增用户地址接口：/api/fans/addAddress,请求参数：{}", dto);
        fansAddressService.addAddress(dto);
        return ApiRes.ok();
    }

    /**
     * 修改用户地址
     **/
    @PostMapping("/updateAddress")
    public ApiRes updateAddress() {
        FansAddressDTO dto = getObject(FansAddressDTO.class);
        Long fansId = getCurrentUser().getFansId();
        log.info("C端修改用户地址接口：/api/fans/updateAddress,请求参数：{},fanId={}", dto,  fansId);
        fansAddressService.updateAddress(dto, fansId);
        return ApiRes.ok();
    }

    /**
     * 查看用户地址详情
     **/
    @PostMapping("/getAddressDetail")
    public ApiRes getAddressDetail() {
        Long fansAddressId = getValLong("fansAddressId");
        Long fansId = getCurrentUser().getFansId();
        log.info("C端查看用户地址详情接口：/api/fans/getAddressDetail,请求参数：fansAddressId={},fanId={}", fansAddressId,fansId);
        FansAddressVO vo = fansAddressService.getAddressDetail(fansAddressId, fansId);
        log.info("C端查看用户地址详情接口：/api/fans/getAddressDetail,返回结果：{}", vo);
        return ApiRes.ok(vo);
    }

    /**
     * 删除用户地址
     **/
    @PostMapping("/delAddress")
    public ApiRes delAddress() {
        Long fansAddressId = getValLong("fansAddressId");
        Long fansId = getCurrentUser().getFansId();
        log.info("C端删除用户地址接口：/api/fans/delAddress,请求参数：fansAddressId={},fanId={}", fansAddressId,  fansId);
        fansAddressService.delAddress(fansAddressId, fansId);
        return ApiRes.ok();
    }
}
